package com.example.carrierapp.ui.adapter;

import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.carrierapp.bean.dispatch.DispatchListBean;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.LogUtil;
import com.example.lb.fingerdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DispatchYesAdapter extends XRecyclerView.Adapter<DispatchYesAdapter.ViewHolder> {
    private List<DispatchListBean.DispatchListInfo> mList = new ArrayList();

    public DispatchYesAdapter() {
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_dispatch_yes, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DispatchListBean.DispatchListInfo info = mList.get(position);
        holder.bindData(info);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends XRecyclerView.ViewHolder {
        @BindView(R.id.tv_address_start)
        TextView mTvStart;
        @BindView(R.id.tv_address_end)
        TextView mTvEnd;
        @BindView(R.id.tv_dispatch_code)
        TextView mTvCode;

        @BindView(R.id.tv_dispatch_goods)
        TextView mTvGoods;
        @BindView(R.id.tv_dispatch_company)
        TextView mTvCompany;
        @BindView(R.id.tv_dispatch_dispatch_log)
        TextView mTvDispatchLog;
        @BindView(R.id.tv_dispatch_arrive_log)
        TextView mTvArriveLog;
        @BindView(R.id.tv_dispatch_detail)
        TextView mTvDetail;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(DispatchListBean.DispatchListInfo info) {
            if (info == null) {
                LogUtil.e(" info is null");
                return;
            }
            mTvStart.setText(info.pickCityName);
            mTvEnd.setText(info.takeCityName);
            mTvCode.setText(info.billCode);
            mTvGoods.setText(info.proName + " 总" + info.thisProNum + info.unitName);
            mTvCompany.setText(info.shipperName);

            mTvDispatchLog.setText("已调度" + info.dispatchedTimes + "车/" + info.dispatchedNum + info.unitName);
            mTvArriveLog.setText("已送达" + info.deliveredTimes + "车/" + info.deliveredNum + info.unitName);

            mTvDetail.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onDetailClick(info);
                }
            });

        }
    }

    public void addDataNotClear(List<DispatchListBean.DispatchListInfo> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<DispatchListBean.DispatchListInfo> list) {
        if (list != null && list.size() > 0) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onDetailClick(DispatchListBean.DispatchListInfo info);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }
}
